package com.orderplus.opshop.synces.service.elasticsearch.impl;

import com.alibaba.fastjson.JSON;
import com.orderplus.opshop.common.util.GsonUtil;
import com.orderplus.opshop.common.util.ValueUtil;
import com.orderplus.opshop.core.data.request.RequestParam;
import com.orderplus.opshop.core.data.response.ResponseData;
import com.orderplus.opshop.core.exception.CheckedException;
import com.orderplus.opshop.core.template.TransTemplate;
import com.orderplus.opshop.synces.bean.ProductDetailModel;
import com.orderplus.opshop.synces.request.OpEsIndexRequest;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

/**
 * author:maihj 2018.9.21
 * <p>
 * OpEsIndexRequest     			原来的请求
 * Boolean     						原来的响应
 * BulkRequest   					转换后的请求
 * ResponseData<Boolean>, Boolean> 	转换后的响应
 */
@Service
@Slf4j
public class OpUpdateService extends TransTemplate<OpEsIndexRequest, Boolean, BulkRequest, ResponseData<Boolean>, Boolean> {
	@Autowired
	private RestHighLevelClient restHighLevelClient;

	@Override
	public ResponseData<Boolean> call(RequestParam<BulkRequest> requestParam) {
		ResponseData<Boolean> responseData = new ResponseData<>();

		BulkRequest bulkRequest = requestParam.getRequestBody();
		try {
			BulkResponse responses = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
			if (responses.hasFailures()) {
				responseData.setData(false);
				System.out.println("es response data:" + responses.buildFailureMessage());
			}
		} catch (IOException e) {
			e.printStackTrace();
			responseData = ResponseData.fail();
			responseData.setData(false);
			return responseData;
		}

		responseData = ResponseData.success();
		responseData.setData(true);

		return responseData;
	}

	@Override
	protected BulkRequest transformRequest(OpEsIndexRequest request) {
//		List<Map<String, Object>> requestList = request.getColumns();
		List<ProductDetailModel> requestList = request.getData();

		log.info("OpUpdateService transformRequest requestData is : " + GsonUtil.toString(requestList));

		if (ValueUtil.isEmpty(requestList)) {
			throw new CheckedException("can not update because Elasticsearch param body is null ");
		}

		BulkRequest bulkRequest = new BulkRequest();

		for (int i = 0; i < requestList.size(); i++) {
//			Map<String, Object> requestParam = requestList.get(i);
//			String id = ValueUtil.toStr(requestParam.get(request.getPrimaryKeyName()));

			ProductDetailModel requestParam = requestList.get(i);
			if (null != requestParam) {
				String id = ValueUtil.toStr(requestParam.getProductId());
				UpdateRequest updateRequest = new UpdateRequest(request.getTableName(), request.getEsType(), id);
				String body = JSON.toJSONString(requestParam);
				log.info("OpUpdateService transformRequest updateRequest body:" + body);

				updateRequest.doc(body, XContentType.JSON);
				bulkRequest.add(updateRequest);
			}
		}

		return bulkRequest;
	}

	@Override
	protected Boolean transformerResponse(Boolean aBoolean, RequestParam<OpEsIndexRequest> requestParam) {
		return true;
	}

}
